VIM 附录 正则表达式匹配

字符 描述
\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个后向引用、或一个八进制转义符。例如,'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。
^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性,^ 也匹配 '\n' 或 '\r' 之后的位置。
$ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性,$ 也匹配 '\n' 或 '\r' 之前的位置。
* 匹配前面的子表达式零次或多次。例如,zo* 能匹配 "z" 以及 "zoo"。 * 等价于{0,}。
+ 匹配前面的子表达式一次或多次。例如,'zo+' 能匹配 "zo" 以及 "zoo",但不能匹配 "z"。+ 等价于 {1,}。
? 匹配前面的子表达式零次或一次。例如,"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。
{n} n 是一个非负整数。匹配确定的 n 次。例如,'o{2}' 不能匹配 "Bob" 中的 'o',但是能匹配 "food" 中的两个 o。
{n,} n 是一个非负整数。至少匹配_n_ 次。例如,'o{2,}' 不能匹配 "Bob" 中的 'o',但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。
{n,m} m 和 n 均为非负整数,其中_n_ <= m。最少匹配 n 次且最多匹配 m 次。刘, "o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。
? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时,匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串,而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如,对于字符串 "oooo",'o+?' 将匹配单个 "o",而 'o+' 将匹配所有 'o'。
. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
(pattern) 匹配_pattern_ 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在Visual Basic Scripting Edition 中则使用 $0…**9使 ' 或 '$'。
(?:pattern) 匹配 pattern 但不获取匹配结果,也就是说这是一个非获取匹配,不进行存储供以后使用。这在使用 "或" 字符 (
(?=pattern) 正向预查,在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如, 'Windows (?=95
(?!pattern) 负向预查,在任何不匹配Negative lookahead matches the search string at any point where a string not matching pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配,也就是说,该匹配不需要获取供以后使用。例如'Windows (?!95
x y
[xyz] 字符集合。匹配所包含的任意一个字符。例如, '[abc]' 可以匹配 "plain" 中的 'a'。
[^xyz] 负值字符集合。匹配未包含的任意字符。例如, '[^abc]' 可以匹配 "plain" 中的'p'。
[a-z] 字符范围。匹配指定范围内的任意字符。例如,'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。
[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符。
\b 匹配一个单词边界,也就是指单词和空格间的位置。例如, 'er\b' 可以匹配"never" 中的 'er',但不能匹配 "verb" 中的 'er'。
\B 匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er',但不能匹配 "never" 中的 'er'。
\c_x_ 匹配由_x_指明的控制字符。例如, \cM 匹配一个 Control-M 或回车符。 x 的值必须为 A-Z 或 a-z 之一。否则,将 c 视为一个原义的 'c' 字符。
\d 匹配一个数字字符。等价于 [0-9]。
\D 匹配一个非数字字符。等价于 [^0-9]。
\f 匹配一个换页符。等价于 \x0c 和 \cL。
\n 匹配一个换行符。等价于 \x0a 和 \cJ。
\r 匹配一个回车符。等价于 \x0d 和 \cM。
\s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\t 匹配一个制表符。等价于 \x09 和 \cI。
\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。
\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。
\x_n_ 匹配 n,其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如, '\x41' 匹配 "A"。'\x041' 则等价于 '\x04' & "1"。正则表达式中可以使用 ASCII 编码。.
\num 匹配 num,其中 num 是一个正整数。对所获取的匹配的引用。例如,'(.)\1' 匹配两个连续的相同字符。
\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式,则 n 为后向引用。否则,如果 n 为八进制数字 (0-7),则 n 为一个八进制转义值。
\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式,则 nm 为后向引用。如果 \nm 之前至少有 n 个获取,则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足,若  n 和 m 均为八进制数字 (0-7),则 \nm 将匹配八进制转义值 nm
\nml 如果 n 为八进制数字 (0-3),且 m 和 l 均为八进制数字 (0-7),则匹配八进制转义值 nml。
\u_n_ 匹配 n,其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如, \u00A9 匹配版权符号 (?)。

1. 正则表达式匹配

正则表达式 有两种, 一种时POSIX 方式, 一种Perl 方式,使用very magic 可以切换到 Perl 方式.
\v 则可以打开
内容:

body    { color: #3c3c3c; }
a        { color: #0000EE; }
strong  { color: #000; }

查找# 号后面 三个 或者6 个 16 进制的数据.

/\v#([0-9a-fA-F]{6}|[0-9a-fA-F]{3})
/\v#(\x{6}|\x{3})

2. 按照原意匹配

/ \Va.k.a.

\V 是按照 字符原本的意思匹配.

:::alert-warning

历史课堂:Vim模式语法的传承
对于Vim的模式来说,除了由 \v\V 开关使能的语法外,还有两种更为古老的语法。Vim缺省使用magic搜索模式,nomagic模式则用于模拟 vi 的行为,可以通过 \m\M开关来分别使能这两种语法。

\M 作为nomagic搜索模式的开关,其功能类似于 \V 原义开关,不同的是,一些字符会自动具有特殊含义,即符号 ^$

magic搜索模式会自动为某些额外的符号赋予特殊含义,如. 、* 以及方括号。magic模式的设计初衷,是想能更容易地构造简单的正则表达式,但它却没能为诸如 +、?、圆括号以及花括号等符号赋予特殊含义,这些符号还必须经过转义才具有特殊含义。

magic搜索模式旨在让构造正则表达式变得容易,但却半途而废,导致哪些字符需要转义的规则制定得比较混乱,难以记忆。\v 模式查找开关正好弥补了这一点,除了 _、数字以及字母外,它为所有符号都赋予了特殊含义。这样既好记,又恰好与Perl正则表达式的规则保持一致。
:::

3. 参考表

h pattern

4. Overview of pattern items                            pattern-overview
                                                E865 E866 E867 E869

Overview of multi items.                                /multi E61 E62
More explanation and examples below, follow the links.          E64 E871

          multi
     'magic' 'nomagic'  matches of the preceding atom
/star   *       \*      0 or more       as many as possible
/\+     \+      \+      1 or more       as many as possible (*)
/\=     \=      \=      0 or 1          as many as possible (*)
/\?     \?      \?      0 or 1          as many as possible (*)

/\{     \{n,m}  \{n,m}  n to m          as many as possible (*)
        \{n}    \{n}    n               exactly (*)
        \{n,}   \{n,}   at least n      as many as possible (*)
        \{,m}   \{,m}   0 to m          as many as possible (*)
        \{}     \{}     0 or more       as many as possible (same as *) (*)

/\{-    \{-n,m} \{-n,m} n to m          as few as possible (*)
        \{-n}   \{-n}   n               exactly (*)
        \{-n,}  \{-n,}  at least n      as few as possible (*)
        \{-,m}  \{-,m}  0 to m          as few as possible (*)
        \{-}    \{-}    0 or more       as few as possible (*)

                                                        E59
/\@>    \@>     \@>     1, like matching a whole pattern (*)
/\@=    \@=     \@=     nothing, requires a match /zero-width (*)
/\@!    \@!     \@!     nothing, requires NO match /zero-width (*)
/\@<=   \@<=    \@<=    nothing, requires a match behind /zero-width (*)
/\@<!   \@<!    \@<!    nothing, requires NO match behind /zero-width (*)

(*) {not in Vi

范围选择:


Overview of ordinary atoms.                             /ordinary-atom
More explanation and examples below, follow the links.

      ordinary atom
      magic   nomagic   matches
/^      ^       ^       start-of-line (at start of pattern) /zero-width
/\^     \^      \^      literal '^'
/\_^    \_^     \_^     start-of-line (used anywhere) /zero-width
/$      $       $       end-of-line (at end of pattern) /zero-width
/\$     \$      \$      literal '


 - 字符类型:

```console
Character classes {not in Vi}:                          /character-classes
/\i     \i      \i      identifier character (see 'isident' option)
/\I     \I      \I      like "\i", but excluding digits
/\k     \k      \k      keyword character (see 'iskeyword' option)
/\K     \K      \K      like "\k", but excluding digits
/\f     \f      \f      file name character (see 'isfname' option)
/\F     \F      \F      like "\f", but excluding digits
/\p     \p      \p      printable character (see 'isprint' option)
/\P     \P      \P      like "\p", but excluding digits
/\s     \s      \s      whitespace character: <Space> and <Tab>
/\S     \S      \S      non-whitespace character; opposite of \s
/\d     \d      \d      digit:                          [0-9]
/\D     \D      \D      non-digit:                      [^0-9]
/\x     \x      \x      hex digit:                      [0-9A-Fa-f]
/\X     \X      \X      non-hex digit:                  [^0-9A-Fa-f]
/\o     \o      \o      octal digit:                    [0-7]
/\O     \O      \O      non-octal digit:                [^0-7]
/\w     \w      \w      word character:                 [0-9A-Za-z_]
/\W     \W      \W      non-word character:             [^0-9A-Za-z_]
/\h     \h      \h      head of word character:         [A-Za-z_]
/\H     \H      \H      non-head of word character:     [^A-Za-z_]
/\a     \a      \a      alphabetic character:           [A-Za-z]
/\A     \A      \A      non-alphabetic character:       [^A-Za-z]
/\l     \l      \l      lowercase character:            [a-z]
/\L     \L      \L      non-lowercase character:        [^a-z]
/\u     \u      \u      uppercase character:            [A-Z]
/\U     \U      \U      non-uppercase character         [^A-Z]
/\_     \_x     \_x     where x is any of the characters above: character
                        class with end-of-line included

特殊类型:

/\e     \e      \e      <Esc>
/\t     \t      \t      <Tab>
/\r     \r      \r      <CR>
/\b     \b      \b      <BS>
/\n     \n      \n      end-of-line
/~      ~       \~      last given substitute string
/\1     \1      \1      same string as matched by first \(\) {not in Vi}
/\2     \2      \2      Like "\1", but uses second \(\)
           ...
/\9     \9      \9      Like "\1", but uses ninth \(\)
                                                                E68
/\z1    \z1     \z1     only for syntax highlighting, see :syn-ext-match
           ...
/\z1    \z9     \z9     only for syntax highlighting, see :syn-ext-match

        x       x       a character with no special meaning matches itself

/[]     []      \[]     any character specified inside the []
/\%[]   \%[]    \%[]    a sequence of optionally matched atoms

/\c     \c      \c      ignore case, do not use the 'ignorecase' option
/\C     \C      \C      match case, do not use the 'ignorecase' option
/\Z     \Z      \Z      ignore differences in Unicode "combining characters".
                        Useful when searching voweled Hebrew or Arabic text.

/\m     \m      \m      'magic' on for the following chars in the pattern
/\M     \M      \M      'magic' off for the following chars in the pattern
/\v     \v      \v      the following chars in the pattern are "very magic"
/\V     \V      \V      the following chars in the pattern are "very nomagic"
/\%#=   \%#=1   \%#=1   select regexp engine /zero-width

/\%d    \%d     \%d     match specified decimal character (eg \%d123)
/\%x    \%x     \%x     match specified hex character (eg \%x2a)
/\%o    \%o     \%o     match specified octal character (eg \%o040)
/\%u    \%u     \%u     match specified multibyte character (eg \%u20ac)
/\%U    \%U     \%U     match specified large multibyte character (eg

/_$ _$ _$ end-of-line (used anywhere) /zero-width
/. . . any single character (not an end-of-line)
/_. _. _. any single character or end-of-line
/< < < beginning of a word /zero-width
/> > > end of a word /zero-width
/\zs \zs \zs anything, sets start of match
/\ze \ze \ze anything, sets end of match
/%^ %^ %^ beginning of file /zero-width E71
/%$ %$ %$ end of file /zero-width
/%V %V %V inside Visual area /zero-width
/%# %# %# cursor position /zero-width
/%'m %'m %'m mark m position /zero-width
/%l %23l %23l in line 23 /zero-width
/%c %23c %23c in column 23 /zero-width
/%v %23v %23v in virtual column 23 /zero-width



 - 字符类型:

{{CODE_BLOCK_7}}

特殊类型:

{{CODE_BLOCK_8}}